window.addEventListener('load', function () {
	//验证码
	var btn = document.querySelector('.btn')
	var time = 10
	btn.onclick = function () {
		btn.disabled = true
		time--
		btn.innerHTML = '还剩' + time + '秒再次发送'
		var timer = setInterval(function () {
			if (time == 0) {
				clearInterval(timer)
				btn.disabled = false
				btn.innerHTML = '发送'
				time = 10
			} else {
				time--
				btn.innerHTML = '还剩' + time + '秒再次发送'
			}
		}, 1000)
	}


	//登录密码
	var psd = document.querySelector('.psd')
	var img = document.querySelector('img')
	var pass = document.querySelector('.pass')
	var flag = true
	img.onclick = function () {
		if (flag == true) {
			img.src = 'images/open.png'
			pass.type = 'text'
			flag = false
		} else {
			img.src = 'images/close.png'
			pass.type = 'password'
			flag = true
		}
	}


	//手机号
	var tel = document.querySelector('.tel')
	var tip = document.querySelector('.tip')
	var tipt = document.querySelector('.tipt')
	tel.onfocus = function () {
		if (tel.value == '请输入手机号码') {
			tel.value = ''

		}
		tel.style.color = '#333'
	}
	tel.onblur = function () {
		if (tel.value == '') {
			tel.value = '请输入手机号码'

		}
		tel.style.color = '#999'
	}

	//手机号验证	
	var re = /^1[3-9]\d{9}$/
	//密码验证
	var repass = /^[a-zA-Z0-9_-]{6,16}$/

	trigger(tel, tip, re)
	trigger(pass, tipt, repass)

	function trigger(ele, nextele, reg) {
		ele.addEventListener('input', function () {
			if (reg.test(this.value)) {
				nextele.firstElementChild.className = 'success'
				nextele.lastElementChild.innerHTML = `格式正确`
				nextele.lastElementChild.className = 'succ_writ'
			} else {
				nextele.firstElementChild.className = 'mistake'
				nextele.lastElementChild.innerHTML = `格式不正确，请重新输入`
				nextele.lastElementChild.className = 'mis_writ'
			}
		})
	}
	//验证密码
	var qpss = document.querySelector('.qpass')
	var tipsan = document.querySelector('.tipsan')
	qpss.addEventListener('input', function () {
		if (this.value === pass.value) {
			tipsan.firstElementChild.className = 'success'
			tipsan.lastElementChild.innerHTML = `密码一致`
			tipsan.lastElementChild.className = 'succ_writ'
		} else {
			tipsan.firstElementChild.className = 'mistake'
			tipsan.lastElementChild.innerHTML = `密码不一致，请重新输入`
			tipsan.lastElementChild.className = 'mis_writ'
		}
	})


	//注册时校验
	var re_btn = document.querySelector('.re_btn');
	re_btn.onclick = function () {
		var succ_writ = document.querySelector('.succ_writ');
		var checked = document.querySelector('#checked')
		// 手机号验证的结果
		var phone = succ_writ.innerHTML   				
		// 密码验证的结果		
		var pwd = tipsan.lastElementChild.innerHTML  			

		if (phone == '格式正确' && pwd == '密码一致') {
			console.log("验证成功")
			tel = tel.value
			pass = pass.value
			if (checked.checked) {
				//1.创建ajax对象
				var xhr = null;
				try {
					xhr = new XMLHttpRequest();
				} catch (error) {
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				// 2.发送请求
				xhr.open("post", "http://localhost:8086/users/", true);
				//告诉服务器发送的是一个什么
				xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded")	

				// 3.调用send
				xhr.send("username=" + tel + "&password=" + pass);

				// 4.等待数据响应
				xhr.onreadystatechange = function () {
					if (xhr.readyState == 4) {
						//判断本次下载的状态码都是多少
						if (xhr.status == 200) {
							console.log(xhr.responseText);
							if (xhr.responseText) {
								alert("恭喜你注册成功")
							} else {
								alert("注册失败")
							}
						} else {
							alert("Error:" + xhr.status);
						}
					}
				}
			}else {
				alert("请同意")
			}

		} else {
			alert('失败')
		}

	}

})
